package com.zzx.threekingdom.view;


import com.zzx.threekingdom.card.Card;

import com.zzx.threekingdom.card.CardList;
import com.zzx.threekingdom.resmanager.ResBitmapLib;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.view.GestureDetector;

public class CardPickerView extends LinearLayout implements View.OnClickListener, GestureDetector.OnGestureListener
{
    public interface onSelectCardListener
    {
        void onSelectCard(Card card);
    }
   
    public onSelectCardListener mListener = null;
    public CardList cardList;
    private GestureDetector mGestureDetector;
    private int leftOffset = 0;
    private Rect mTouchFrame = new Rect();
    private View mPrePressedChildView = null;
    
    public CardPickerView(Context context)
    {
        this(context, null);
    }
    public CardPickerView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    public CardPickerView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, this);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.LEFT);
    }
    
    public CardPickerView addViewByCards(CardList cards)
    {
        int size = cards.size();
        for(int count = 0;count<size;count++)
        {
            CardView cv = new CardView(getContext(), cards.get(count));
            addView(cv);
        }
        return this;
    }
    
    public CardPickerView setCardList(CardList c)
    {
        removeAllViews();
        cardList = c;
        addViewByCards(c);
        return this;
    }
    public CardPickerView addCardList(CardList c)
    {
        cardList.addCards(c);
        addViewByCards(c);
        return this;
    }
    
    public void onClick (View v)
    {
        if(mListener!=null)
        {
            if(v instanceof CardView)
                mListener.onSelectCard(((CardView)v).card);
        }
    }
    public CardPickerView setSelectListener(onSelectCardListener listener)
    {
        mListener = listener;
        return this;
    }
    
    public int getViewsWidth()
    {
        if(cardList!=null)
            return cardList.size()*(ResBitmapLib.CARD_WIDTH + 2);
        return 0;
    }
    
    public int getViewsHeight()
    {
        return (ResBitmapLib.CARD_HEIGHT + 2);
    }
    
    public boolean isScrollable()
    {
        return (getWidth()<getViewsWidth());
    }
    

    public void leftOffsetChild(int toLeft)
    {
        if(isScrollable()==false)
            return;
        final int leftOffsetMax = getViewsWidth() - getWidth();
        if(leftOffset<0||leftOffset>leftOffsetMax)
            leftOffset = 0;
        int newToLeft = leftOffset - toLeft;
        
        if((toLeft>0)&&(newToLeft<0))
            toLeft = leftOffset;
        else if((toLeft<0)&&(newToLeft>leftOffsetMax))
            toLeft = leftOffset - leftOffsetMax;
        
        leftOffset -= toLeft;
        int childCount = getChildCount();
        for (int count = 0; count < childCount; count++)
        {
            getChildAt(count).offsetLeftAndRight(toLeft);
        }
        
        invalidate();
    }
    
    @Override
    public boolean onTouchEvent (MotionEvent event)
    {
        boolean retValue = mGestureDetector.onTouchEvent(event);
        int action = event.getAction();
        if(action==MotionEvent.ACTION_UP)
            onUp(event);
        else if(action==MotionEvent.ACTION_MOVE)
            onMove(event);
        
        return retValue;
    }

    public boolean onDown(MotionEvent e)
    {
        View childView = pointToView((int)e.getX(), (int)e.getY());
        if(childView!=null)
        {
            childView.setPressed(true);
            if(mPrePressedChildView!=null)
            {
                mPrePressedChildView.setPressed(false);
                mPrePressedChildView.invalidate();
            }
            mPrePressedChildView = childView;
            childView.invalidate();
        }
        return true;
    }
    public boolean onUp(MotionEvent e)
    {
        if(mPrePressedChildView!=null)
        {
            mPrePressedChildView.setPressed(false);
            onClick(mPrePressedChildView);
            mPrePressedChildView.invalidate();
        }
        mPrePressedChildView = null;
        return true;
    }
    public boolean onMove(MotionEvent e)
    {
        if(mPrePressedChildView!=null)
        {
            final int x = (int) e.getX();
            final int y = (int) e.getY();

            mPrePressedChildView.getHitRect(mTouchFrame);
            if (mTouchFrame.contains(x, y)==false)
            {
                if(mPrePressedChildView!=null)
                {
                    mPrePressedChildView.setPressed(false);
                    mPrePressedChildView.invalidate();
                }
                mPrePressedChildView = null;
            }
        }
        return true;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        // TODO Auto-generated method stub
        return true;
    }


    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        if(distanceX!=0)
            leftOffsetChild(-1 * (int) distanceX);
        return true;
    }

    public boolean onSingleTapUp(MotionEvent e)
    {
        // TODO Auto-generated method stub
        return false;
    }

    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub
        
    }
    
    public View pointToView(int x, int y)
    {
        final int count = getChildCount();
        for (int i = count - 1; i >= 0; i--)
        {
            View child = getChildAt(i);
            if (child.getVisibility() == View.VISIBLE)
            {
                child.getHitRect(mTouchFrame);
                if (mTouchFrame.contains(x, y))
                    return child;
            }
        } 
        return null;
    }

}